package com.example.demo.auth;

import org.apache.commons.lang.StringUtils;

public class ClientToken {
	private String appid;
	private String secret;
	private String token;
	private long expire;
	
	private String timestamp;
	private String nonce;
	private String sign;
	
	
	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getNonce() {
		return nonce;
	}

	public void setNonce(String nonce) {
		this.nonce = nonce;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	/**
	 * 验证时间 不能超过3分钟
	 * @return
	 */
	public boolean checkTimestamp() {
		if (timestamp == null || "".equals(timestamp)) {
			return false;
		}
		
		long now = System.currentTimeMillis() / 1000;
		Long timestamp_ = Long.valueOf(timestamp);
		
		if (((now - timestamp_) <= 180 && (now - timestamp_) >= 0)
				|| ((now - timestamp_) <= 0 && (now - timestamp_) >= -180)) {
			return true;
		} else {
			return false;
		}
	
	}
	
	/**
	 * 验证随机数
	 * @return
	 */
	public boolean checkNonce() {
		if(StringUtils.isBlank(nonce)) {
			return false;
		}
		return true;
	}
	
	/**
	 * 验证签名
	 * @param signUrlEncode 
	 * @return
	 */
	public boolean checkSign(String signUrlEncode) {
		if(StringUtils.isNotBlank(sign) && 
				StringUtils.isNotBlank(signUrlEncode) && signUrlEncode.equals(sign)) {
			return true;
		}
		return false;
	}
	
	
	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public long getExpire() {
		return expire;
	}

	public void setExpire(long expire) {
		this.expire = expire;
	}

	/**
	 * 是否过期
	 * @return true已过期
	 */
	public boolean isExpired() {
		return getExpire() < (System.currentTimeMillis()/1000);
	}
	

	/**
	 * 是否未过期 
	 * @return true 未过期
	 */
	public boolean notExpired() {
		return ! isExpired();
	}



	@Override
	public String toString() {
		return JsonKit.general.toJson(this);
	}

	
	
}
